﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;


namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class TR_Trainers3 : System.Web.UI.UserControl
    {
        private object _dataItem = null;
        private InitialData_BLL iniData = new InitialData_BLL();
        private MasNamePrefixBLL namePre = new MasNamePrefixBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        private ComboBLL comboBLL = new ComboBLL();

        //ReadOnly property.
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                bindEmp();
                bindPrefixName();
                this.ViewState.Add("IsUserControlPostBack", true);

                RadTreeView tree = (RadTreeView)ddlDis.Items[0].FindControl("RadTreeView1");
                LoadRootNodes(tree, TreeNodeExpandMode.ServerSideCallBack);
            }

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblID.Text = DataBinder.Eval(DataItem, "training_subject_seq").ToString();
                    txtName.Text = DataBinder.Eval(DataItem, "training_subject_descr").ToString();

                    this.btnAddnew.Visible = false;
                    this.btnUpdate.Visible = true;
                }
                else
                {
                    this.btnUpdate.Visible = false;
                    this.btnAddnew.Visible = true;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void bindPrefixName()
        {
            IList<MasNamePrefixInfo> infoList = namePre.ListNamePrefixes();
            ddlPrefix.DataSource = infoList;
            ddlPrefix.DataBind();
        }

        protected void bindEmp()
        {
            IList<EmployeeInfo> infoList = empBLL.ListEmployeeByTypes("1", string.Empty, string.Empty);

            foreach (EmployeeInfo List in infoList)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = List.Person.NamePrefix.Name + List.Person.FirstName + " " + List.Person.LastName;
                item.Value = List.Sequence.ToString();

                item.Attributes.Add("EMP_NAME", List.Person.NamePrefix.Name + List.Person.FirstName + " " + List.Person.LastName);
                item.Attributes.Add("EMP_NAME_ENG", List.Person.FirstNameEng + " " + List.Person.LastNameEng);
                //item.Attributes.Add("POS_NAME", List.Position == null ? "" : List.Position.Name);
                //item.Attributes.Add("CLASS_NAME", List.Class.Name);

                cmbEmployee.Items.Add(item);
                item.DataBind();
            }
        }

        protected void checkStaEmp(string status)
        {
            if (status == "out")
            {
                rdoEmpIn.Checked = false;
                rdoEmpOut.Checked = true;
                ddlPrefix.Enabled = true;
                txtName.Enabled = true;
                txtLastName.Enabled = true;
                txtAddress.Enabled = true;
               // txtDistrict.Enabled = true;
                txtPostCode.Enabled = true;
                txtTel.Enabled = true;
               /// btnFindDistrict.Visible = true;
            }
            else
            {
                rdoEmpOut.Checked = false;
                rdoEmpIn.Checked = true;
                ddlPrefix.Enabled = false;
                txtName.Enabled = false;
                txtLastName.Enabled = false;
                txtAddress.Enabled = false;
             //   txtDistrict.Enabled = false;
                txtPostCode.Enabled = false;
                txtTel.Enabled = false;
             //   btnFindDistrict.Visible = false;
            }
        }



        private EmployeeInfo empInfo;
        protected void bindForEdit(string seq)
        {

        }

        protected void rdoEmpOut_CheckedChanged(object sender, EventArgs e)
        {
            checkStaEmp("out");
        }

        protected void btnSelectItem_Click(object sender, EventArgs e)
        {
            checkStaEmp("in");
            IList<EmployeeInfo> empList = empBLL.ListEmployees(cmbEmployee.SelectedValue);
            if (empList.Count > 0)
            {
                empInfo = empList[0];
                ddlPrefix.SelectedValue = empInfo.Person.NamePrefix.Sequence.ToString();
                txtName.Text = empInfo.Person.FirstName != null ? empInfo.Person.FirstName : "";
                txtLastName.Text = empInfo.Person.LastName != null ? empInfo.Person.LastName : "";
                //txtAddress.Text = empInfo.Address != null ? empInfo.Address.Address : "";
                ////txtDistrict.Text = empInfo.Address
                //txtPostCode.Text = empInfo.Address.PostalCode != null ? empInfo.Address.PostalCode : "";
                //txtTel.Text = empInfo.Address.Phone != null ? empInfo.Address.Phone : "";
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            IList<EmployeeInfo> empList = empBLL.ListEmployees(cmbEmployee.SelectedValue);
            if (empList.Count > 0)
            {
                empInfo = empList[0];
                ddlPrefix.SelectedValue = empInfo.Person.NamePrefix.Sequence.ToString();
                txtName.Text = empInfo.Person.FirstName != null ? empInfo.Person.FirstName : "";
                txtLastName.Text = empInfo.Person.LastName != null ? empInfo.Person.LastName : "";
                //txtAddress.Text = empInfo.Address != null ? empInfo.Address.Address : "";
                ////txtDistrict.Text = empInfo.Address
                //txtPostCode.Text = empInfo.Address.PostalCode != null ? empInfo.Address.PostalCode : "";
                //txtTel.Text = empInfo.Address.Phone != null ? empInfo.Address.Phone : "";
            }
        }

        protected void btnFindDistrict_Load(object sender, EventArgs e)
        {
            // btnFindDistrict.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogEmp.aspx?ETID=" + ETID + "','../frmIframe.html',700,500)");
        }

        protected void btnFindDistrict_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                //string ss = cmbEmployee.SelectedValue;
                iniData.InsertTrainingSubject(txtName.Text, "1");
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                iniData.UpdateTrainingSubject(lblID.Text, txtName.Text, "1");
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }



        private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            foreach (DataRow info in comboBLL.ListDistrictTree("").Rows)
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info["NAME"].ToString();
                node.Category = info["Category"].ToString();
                node.ToolTip = info["DESCRIPTION"].ToString();
                node.Value = info["ID"].ToString();
                node.ImageUrl = info["IMAGE_URL"].ToString();
                node.Checkable = false;

                if (Convert.ToInt32(info["CHILDREN_COUNT"]) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                treeView.Nodes.Add(node);
            }
        }
        protected void TreeItem_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);
        }

        private void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            e.Node.Dispose();
            e.Node.Nodes.Clear();

            foreach (DataRow info in comboBLL.ListDistrictTree(e.Node.Value).Rows)
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info["NAME"].ToString();
                node.Category = info["Category"].ToString();
                node.ToolTip = info["DESCRIPTION"].ToString();
                node.Value = info["ID"].ToString();
                node.ImageUrl = info["IMAGE_URL"].ToString();
                node.Checkable = (info["Category"].ToString() == "ITEM");

                if (Convert.ToInt32(info["CHILDREN_COUNT"]) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }
            e.Node.Expanded = true;
            e.Node.ExpandMode = expandMode;
        }









    }
}